package com.controller;

import com.pojo.Supplier;
import com.service.SupplierService;
import com.util.Page;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/supplier")
public class SupplierController {

    private static final Logger logger=Logger.getLogger(SupplierController.class);
    @Resource
    private SupplierService supplierService;

    @RequestMapping("/list")
    public String getSupplierList(Model model, String querySupName,
                                  String querySupCode, @RequestParam(defaultValue = "1") int pageIndex,Integer pageSize){
        List<Supplier> supplierList=null;
        Page<Supplier> supplierPage = new Page<Supplier>();
        try {
            //设置总数据量
            supplierPage.setTotalPage(supplierService.count(querySupCode, querySupName));
            //设置总页数
            supplierPage.setTotalPageSize();
            //当前页
            supplierPage.setPageIndex(pageIndex);
            //获取当前页数据
            supplierPage.setList(supplierService.select(querySupCode, querySupName, supplierPage.getPageIndex(), supplierPage.getPageSize()));
            supplierList=supplierService.select(querySupCode,querySupName,pageIndex,pageSize);
            model.addAttribute("supplierList",supplierList);
            model.addAttribute("querySupCode", querySupCode);
            model.addAttribute("querySupName", querySupName);
            model.addAttribute("supplierList", supplierPage.getList());
            model.addAttribute("totalPageCount", supplierPage.getTotalPageSize());
            model.addAttribute("totalCount", supplierPage.getTotalPage());
            model.addAttribute("currentPageNo", supplierPage.getPageIndex());
        }catch (Exception e){
            e.printStackTrace();
            logger.error("供货商列表接口访问失败!");
            return "redirect:/supplier/toSysError";
        }
        return "supplier/list";
    }
    @RequestMapping(value = "/toSysError")
    public String sysError(){
        return "sysError";
    }
}

